﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.Serialization;
using System.IO;
using helper = CommonT4.HelperExtensionT4;

namespace SerializableTypesT4
{
	enum ApbPhaseOut
	{
		direct = 0,
		 indirect = 2, last = 3,
	}
	class Run
	{
		internal static Assembly srcAssembly;		
		/// <summary>
		/// Serializable Types: <see cref="http://msdn.microsoft.com/en-us/library/cc656732.aspx"/>
		/// Why TextTemplatingFilePreprocessor and placing helper methods in .cs files,\
		/// not .tt or include files? Easier debugging, more reliable environment variables, easier assembly referencing.
		/// </summary>
		/// <param name="args"></param>
		static void Main(string[] args)
		{
			
			var settings = SerializableTypesT4.Properties.Settings.Default;
			srcAssembly = Assembly.LoadFile(settings.SrcAssembly);
			var excluded = settings.ExcludeTypeFullNameSubstrings.Cast<string>();
			var template = new SerializableTypesT4.SerializableTypesTemplate
			{
				SrcAssembly = srcAssembly, 
				TypeMappings = helper.CreateMappings(settings.SrcTypeMappings.Cast<string>(), settings.DestTypeMappings.Cast<string>()),
				NamespaceMappings = helper.CreateMappings(settings.SrcNamespaceMappings.Cast<string>(), settings.DestNamespaceMappings.Cast<string>()),
				ExcludeTypeFullNameSubstrings = excluded,
				MustExcludeSrcTypes = null,
				RequiredBaseTypes = null
			};
			var content = template.TransformText();
			foreach (var path in settings.outputPaths.Cast<string>())
			{
				FileInfo f = new FileInfo(path);
				if (f.Exists)
					f.IsReadOnly = false;
				//@"E:\CACI\UnitTests\BusinessObjects.SL\BusinessObjects-Serializable.cs"
				System.IO.File.WriteAllText(path, content);
			}
			
		}

		/// <summary>
		/// 2-step process of (1) creating the Serializable Types C# files, 
		/// and (2) hence using the created Assembly from (1) along with the initial source assembly,
		/// to create the ConvertExtension methods.
		/// </summary>
	
		/// <summary>
		/// (a) Use for KnownTypes attribute on the WCF service? (derives from IObjectService), or
		/// (b) KnownTypes for every Type that might need to be serialized; probably all classes/structs.
		/// </summary>				
        
	}
}
